package dtu.app.activity;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;

import com.iqengines.demo.R;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

import dtu.app.fragment.CategoryListFragment;
import dtu.app.fragment.ProductListFragment;
import dtu.app.fragment.CategoryListFragment.OnCategoryItemSelectedListener;

public class BaseActivity extends SlidingFragmentActivity implements OnCategoryItemSelectedListener, ProductListFragment.UpdateCallback {

	String mCurrentCategory;
	protected CategoryListFragment mFragCategory;
	protected ProductListFragment mFragProduct;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitle(R.string.app_name);
		setBehindContentView(R.layout.menu_frame);
		setContentView(R.layout.content_frame);
		setSlidingActionBarEnabled(false);

		if (savedInstanceState == null) {
			FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
			mFragCategory = new CategoryListFragment();
			t.replace(R.id.menu_frame, mFragCategory);

			mFragProduct = new ProductListFragment();
			t.replace(R.id.content_frame, mFragProduct);
			t.commit();
		} else {
			mFragCategory = (CategoryListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
			mFragProduct = (ProductListFragment)this.getSupportFragmentManager().findFragmentById(R.id.content_frame);
		}

		// customize the SlidingMenu
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		int width = size.x;

		int width20 = (int) (width * 0.25);

		SlidingMenu sm = getSlidingMenu();
		sm.setBehindWidth(width20);
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		//sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		//		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public void onCategoryItemSelected(String category) {
		Log.i("Category", category);
		ProductListFragment list = (ProductListFragment) getSupportFragmentManager().findFragmentById(R.id.content_frame);
		list.updateList(category);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menu_scan: startActivity(new Intent(this, ScanActivity.class)); break;
		case R.id.menu_temp_products: startActivity(new Intent(this, TempProductActivity.class)); break;
		default: break;
		}

		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		
		mFragCategory.updateList();
		if (null != mCurrentCategory)
			mFragProduct.updateList(mCurrentCategory);
	}

	@Override
	public void update(String category) {
		mFragCategory.updateList();
	}
}
